package com.dg.lecheng.api.user.service;

import com.dg.lecheng.api.user.model.InfoRole;
import com.dg.lecheng.api.user.repo.InfoRoleDao;
import com.dg.lecheng.base.core.service.CrudService;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class InfoRoleService extends CrudService<InfoRoleDao, InfoRole, Integer> {
    @Autowired
    private InfoRoleAuthService infoRoleAuthService;
    @Autowired
    private InfoUserRoleService infoUserRoleService;

    public int getCount(String name, String code) {
        return curdRepo.getCount(name, code);
    }

    public List<InfoRole> findByName(String name, PageBounds pageBounds){
        return curdRepo.findByName(name,pageBounds);
    };

    /**
     * 删除角色信息，同时删除 【角色对应的资源，角色对应的用户】 的对应关系
     * @param id
     */
    @Transactional("apiTM")
    public void removeRoleAndRelatedInfo(int id){
        curdRepo.remove(id);
        /**
         * 删除 与资源、用户的对应关系
         */
        infoRoleAuthService.removeByRoleId(id);
        infoUserRoleService.removeByRoleId(id);
    }

    public List<InfoRole> findAll(){
        return curdRepo.findAll();
    }
}